package com.fingard.dsp.bank.directbank.egbank01;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class EGBANK01Transaction extends EGBANK01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //历史明细查询返回ATS的对象
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {

            Calendar tmpCalendarFrom = Calendar.getInstance();
            tmpCalendarFrom.setTime(p_req7516.getDateFrom());

            Calendar tmpCalendarTo = Calendar.getInstance();
            tmpCalendarTo.setTime(p_req7516.getDateTo());

            int tmpIntDateFrom = 0;
            int tmpIntDateTo = 0;
            //按自然月循环查询
            while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
                Calendar tmpClndrStepTo = Calendar.getInstance();
                tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
                //判断是否跨月
                if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
                    //跨月则设置为月末
                    tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
                } else {
                    tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
                }

                int queryPageNum = 1;
                int totalPageNum = 1;
                while (queryPageNum <= totalPageNum) {
                    EGBANK01Header tmpHeader = new EGBANK01Header();
                    tmpHeader.opName = "queryTranDetailOp";
                    tmpHeader.serialNo = getQryTransID();
                    tmpHeader.needSigned = false;

                    XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                    //写入通用的头部节点
                    writeCommonHeader(xmlWriter, tmpHeader);
                    xmlWriter.writeElementString("cstNo", bankFront.loginName);
                    xmlWriter.writeStartElement("reqParam");
                    xmlWriter.writeElementString("ACNO", p_req7516.actNum);
                    xmlWriter.writeElementString("BIZH", "");
                    xmlWriter.writeElementString("QSRQ", Format.Date8Format.format(tmpCalendarFrom.getTime()));
                    xmlWriter.writeElementString("ZZRQ", Format.Date8Format.format(tmpClndrStepTo.getTime()));
                    xmlWriter.writeElementString("turnPageBeginPos", String.valueOf(queryPageNum));
                    xmlWriter.writeElementString("LTSN", "");
                    xmlWriter.writeElementString("RNUM", "50");
                    xmlWriter.writeElementString("JEDA", "");
                    xmlWriter.writeElementString("MIFS", "0");
                    xmlWriter.writeElementString("MAFS", "99999999999999.99");
                    xmlWriter.writeEndDocument();
                    String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpHeader);
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                        if (tmpBkReader != null) {
                            String tmpRetCode = tmpBkReader.getFirstText("/HFBankData/opRep/retCode");
                            String tmpRetInfo = tmpBkReader.getFirstText("/HFBankData/opRep/errMsg");
                            if (tmpRetCode != null && tmpRetCode.equals("0")) {
                                if (totalPageNum == 1) {
                                    int totalRecord = Integer.parseInt(tmpBkReader.getFirstText("/HFBankData/opRep/opResult/TTTS"));
                                    totalPageNum = totalRecord / 50;
                                    if (totalRecord % 50 > 0) {
                                        totalPageNum++;
                                    }
                                }

                                while (tmpBkReader.read("/HFBankData/opRep/opResultSet/opResult")) {
                                    RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
                                    XElement tmpXElement = tmpBkReader.readedXElement;
                                    tmpRetDetail.actNum = p_req7516.actNum;
                                    tmpRetDetail.actArea = p_req7516.actArea;
                                    tmpRetDetail.urid = tmpXElement.getSingleText("SEQU");
                                    String transDate = tmpXElement.getSingleText("HPDT");
                                    if (transDate.length() == 8) {
                                        tmpRetDetail.tranDate = transDate.substring(0, 4) + "-" + transDate.substring(4, 6) + "-" + transDate.substring(6, 8);
                                    } else {
                                        tmpRetDetail.tranDate = transDate;
                                    }

                                    tmpRetDetail.valueDate = tmpRetDetail.tranDate;
                                    tmpRetDetail.tranTime = tmpRetDetail.tranDate + " 00:00:00";
                                    String cdFlag = tmpXElement.getSingleText("JDFX");
                                    String amount = tmpXElement.getSingleText("FASE");
                                    if (cdFlag.equals("C")) {
                                        if (amount.contains("-")) {
                                            tmpRetDetail.cdFlag = "1";
                                        } else {
                                            tmpRetDetail.cdFlag = "2";
                                        }

                                    } else if (cdFlag.equals("D")) {
                                        if (amount.contains("-")) {
                                            tmpRetDetail.cdFlag = "2";
                                        } else {
                                            tmpRetDetail.cdFlag = "1";
                                        }
                                    } else {
                                        tmpRespCode = "-1";
                                        tmpRespInfo = String.format("银行返回借贷标识%s无法核对，请确认！", cdFlag);
                                    }
                                    tmpRetDetail.tranAmt = amount.replace("-", "");
                                    tmpRetDetail.accBal = tmpXElement.getSingleText("THYE");
                                    tmpRetDetail.comment = tmpXElement.getSingleText("ZHYO");
                                    tmpRetDetail.originator = tmpXElement.getSingleText("OPER");
                                    tmpRetDetail.recAccNum = tmpXElement.getSingleText("RANO");
                                    tmpRetDetail.recAccName = tmpXElement.getSingleText("RANM");
                                    tmpRetDetail.recAccBank = tmpXElement.getSingleText("RCBN");
                                    tmpRetDetail.usage = tmpXElement.getSingleText("BEZU");
                                    tmpRetDetail.abstr = pickupAbstr(tmpRetDetail.usage, tmpRetDetail.comment);
                                    retAts.writeRetDetail(tmpRetDetail);


                                }
                                queryPageNum++;
                            } else {
                                tmpRespCode = "-1";
                                tmpRespInfo = tmpRetInfo;
                                break;
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpSbMsg.toString();
                            break;
                        }

                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpStrRet[1];
                        break;
                    }
                }


                tmpCalendarFrom = tmpClndrStepTo;
                tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
                tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
                tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("交易为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
        tmpReq7516.actArea = p_req7515.actArea;
        tmpReq7516.actCur = p_req7515.actCur;
        tmpReq7516.actNum = p_req7515.actNum;
        String tmpStrToday = Format.Date8Format.format(new Date());
        tmpReq7516.dateFrom = tmpStrToday;
        tmpReq7516.dateTo = tmpStrToday;
        return d_7516_HisTrans(tmpReq7516);
    }
}
